package com.szxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.szxy.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zwz
 * @Date:2020/7/3 6:20
 * @Description:com.szxy.controller
 * @Version:1.0
 * 流控测试
 **/
@RestController
public class TestController {

    /**
     * QPS 每秒请求数
     * @return
     */
    @GetMapping("/test")
    public ResponseEntity<String> test() {
        return ResponseEntity.ok("OK");
    }


    /**
     * 线程数 阈值
     * @return
     */
    @GetMapping("/thread")
    public ResponseEntity<String> thread() throws InterruptedException {
        // 睡眠一秒，模拟执行正常的业务
        // Thread.sleep(1000);
        TimeUnit.SECONDS.sleep(1);
        return ResponseEntity.ok("thread");
    }

    /**
     * 测试关联1
     * 当访问 /test2 超过阈值时，对 /test1 进行限流
     * @return
     */
    @GetMapping("/test1")
    public ResponseEntity<String> test1() {
        return ResponseEntity.ok("test1");
    }

    /**
     * 测试关联2
     * 当访问 /test2 超过阈值时，对 /test1 进行限流
     * @return
     */
    @GetMapping("/test2")
    public ResponseEntity<String> test2() {
        return ResponseEntity.ok("test2");
    }

    @Autowired
    private TestService testService;

    /**
     * 链路1
     * @return
     */
    @GetMapping("/link1")
    public ResponseEntity<String> link1() {
        String hello = this.testService.hello();
        System.out.println("hello1 = " + hello);
        return ResponseEntity.ok("link1 -> hello -> others");
    }

    /**
     * 链路2
     * @return
     */
    @GetMapping("/link2")
    public ResponseEntity<String> link2() {
        String hello = this.testService.hello();
        System.out.println("hello2 = " + hello);
        return ResponseEntity.ok("link2 -> hello -> others");
    }

    /**
     * 日志打印
     */
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    /**
     * 排队
     * @return
     */
    @GetMapping("/queue")
    public ResponseEntity<String> quence() {
        logger.info("开始处理 ....");
        return ResponseEntity.ok("quence");
    }

    /**
     * rt 平均响应时间
     *
     * @return
     */
    @GetMapping("/rt")
    public ResponseEntity<String> rt() throws InterruptedException {
        // 睡眠一秒
        TimeUnit.SECONDS.sleep(1);
        return ResponseEntity.ok("rt");
    }


    /**
     * 异常比例 、 异常数量
     *
     * @return
     */
    @GetMapping("/exception")
    public ResponseEntity<String> exception() throws InterruptedException {
        throw new RuntimeException("测试异常比列，异常数量 Exception ....");
    }


    /**
     * 热点规则： 可以精确到参数值是什么
     * 根据给定参数值，确定限流规则
     * @return
     */
    @GetMapping("/buy")
    @SentinelResource("bug")
    public ResponseEntity<String> bug(String prodName, int num) throws InterruptedException {
        return ResponseEntity.ok(String.format("购买 %s， 共 %s 件", prodName, num));
    }


}
